from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy import and_, or_, not_
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:000000@localhost/demo'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)


# db.init_app(app)
# class MyModel(db.Model):


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uesrname = db.Column(db.String(16), unique=True, nullable=False)
    password = db.Column(db.Integer, unique=False, nullable=False)

    def __repr__(self):
        return f'<User {self.uesrname}>'


Migrate(app,db)


@app.route('/')
def index():
    # users = User.query.filter_by(uesrname='123456').first()
    # if users:
    #     print(users)
    #     print(users.password)

    # user = (User.query.get(1)) # 获取主键的数据
    # user.password = 234567  # 修改数据
    # db.session.commit() # 提交操作

    # user = (User.query.get(1))
    # db.session.delete(user)  # 删除对象
    # db.session.commit()  # 提交操作

    # and 逻辑
    #
    # users = User.query.filter(and_(User.uesrname=='223',User.password=='223')).all()
    # if users:
    #     print(users)

    # from sqlalchemy import and_, or_, not_
    # users = User.query.filter(or_(User.uesrname == '223', User.password == '123')).all()
    # if users:
    #     print(users)

    # users = User.query.filter(not_(User.uesrname == '555')).all()
    # if users:
    #     print(users)

    # 筛选出部分数据
    # users = User.query.filter(User.uesrname.in_(['223','555'])).all()
    # if users:
    #     print(users)

    return 'hello:search'



if __name__ == '__main__':
    app.run(debug=True)
